import get from '@dfeidao/atom-web/local/get';
import set from '@dfeidao/atom-web/local/set';
import fire from '@dfeidao/atom-web/msg/fire';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import error from '@dfeidao/atom-web/msgbox/error';
import success from '@dfeidao/atom-web/msgbox/success';
import { IFeidaoAiBrowserComponent } from '@dfeidao/web/interfaces';
import hide_loading from '../../atom/hide-loading';
import show_loading from '../../atom/show-loading';

export default async function a002(fd: IFeidaoAiBrowserComponent, args: { currentTarget: HTMLElement }) {
	//  保存

	const dom = args.currentTarget; // 获取DOM
	const stw = dom.getAttribute('data-feidao-switch'); // 获取开关的属性值
	if (stw === 'false') {   // 判断开关状态
		error('正在保存中');
		return;
	}
	set(fd, 'sss', 1);
	dom.setAttribute('data-feidao-switch', 'false');// 在调用保存服务之前改变开关状态为false
	const pk_val = get(fd, 'pk_val');
	const search_field_val = get(fd, 'search_field_val');
	const role_no = get(fd, 'role_no');
	const ff = get(fd, 'ff');
	const productid = get(fd, 'productid') as string;

	if (!role_no) {
		dom.setAttribute('data-feidao-switch', 'true');// 在拿到结果之后改变开关状态为true
		error('角色编号为空');
		return;
	}
	if (!pk_val) {
		dom.setAttribute('data-feidao-switch', 'true');// 在拿到结果之后改变开关状态为true
		error('请选择一行数据');
		return;
	}
	const query = {
		pk_val,
		search_field_val,
		role_no,
		productid
	};
	// const res = await nodejs(url, './permissions/pub_role_allot_add', query, false, false);
	// debugger;
	show_loading();
	const res = await nodejs<{ ret: string; msg: string; }>('roler/service/pub_role_allot_add', query);
	hide_loading();
	if (res.ret === 'success') {
		dom.setAttribute('data-feidao-switch', 'true');// 在拿到结果之后改变开关状态为true
		fire('zj-000011', 'ec-002', role_no, ff);
		fire('zj-000012', 'eu-001');
		success('保存成功');
	} else {
		dom.setAttribute('data-feidao-switch', 'true');// 在拿到结果之后改变开关状态为true
		error(res.msg);
	}
}
